jQuery(document).ready(function($) {
$("#header").load("/job/topul.html");

    pType();
    showAll();

    //查询所有职位类型
    function pType(){
        //请求
        $.ajax({
            url : "position/showAllPositionType",
            type : "get",
            dataType : "json",
            success : function(data){
                $(data.data.dictValue).each(function(){
                    $("#positionType").append(" <option name='positionType' value='"+this.id+"'>"+this.label+"</option>")
                    $("#pid").append(" <option name='pid' value='"+this.id+"'>"+this.label+"</option>")
                    $("#pid2").append(" <option name='pid' value='"+this.id+"'>"+this.label+"</option>")

                })
            }
        })
        //请求end
    }
    //职位类型查询end


    //所有职位查询请求
    function showAll() {
        //请求
        $.ajax({
            url: "position/showAllByCreateById",
            type: "get",
            dataType: "json",
            success: function (data) {
                $("#positionTbody").empty();
                $(data.data).each(function (j) {
                    $("#positionTbody").append(" <tr><td>"
                        + (j + 1) + "</td><td>"
                        + this.name + "</td><td>"
                        + this.city + "</td><td>"
                        + "<span class='count'>"
                        +this.minSalary+"</span>" + "~" + "<span class='count'>"
                        +this.maxSalary+"</span>" + "</td><td>"
                        + this.education + "</td><td><button type='button' class='btn btn-outline-success btn-sm' name='positionDetails'"
                        + "id='"+this.address+"_"+this.detail+"_"+this.workedyears+"_"+this.skillList+"_"+this.requiredNum+"_"
                        +"'>详情</button></td><td><button type='button' class='btn btn-outline-warning btn-sm' name='positionEdit' id='"
                        +this.id+"_"+this.name+"_"+this.minSalary+"_"+this.maxSalary+"_"+this.city+"_"+this.address+"_"+this.workedyears+"_"+this.education+"_"+this.requiredNum+"_"+this.detail+"_"+this.skillList+"'>修改</button>&nbsp;&nbsp;<button type='button' class='btn btn-outline-danger btn-sm' name='positionDelete'"
                        + "id='"+ this.id +"'>删除</button></td>"
                    )
                })
            },
            error: function (err) {
                alert("失败")
            }
        })
        //请求end
    }
    //所有职位查询请求end

    //添加职位
    $("#positionAdd").on("click", function(){
//        let skillList = new Array();
//        let skills = $("#skills").val()
//        skillList=skills.split(";");

        var Data = {"name":$("#name").val(), "minSalary":$("#minSales").val(), "maxSalary":$("#maxSales").val(),
            "city":$("#city").val(), "address":$("#address").val(), "pid":$("#pid").val(),
            "workedyears":$("#workedyears").val(), "education":$("#education").val(),
            "requiredNum":$("#requiredNum").val(),"detail":$("#detail").val(),"skillList":$("#skills").val()}
        //请求
        $.ajax({
            url : "position/addPosition",
            data : JSON.stringify(Data),
            type : "post",
            contentType: 'application/json;charset=utf-8',
            dataType : "json",
            success : function(data){
                alert("成功")
                $("#positionAddModal").modal("hide")
                showAll();
            }
        })
        //请求end
    })
    //添加职位end

    //职位详情
    $("body").on("click", "button[name='positionDetails']", function(){
        let data = $(this).attr("id").split("_")

        $("#address2").val(data[0])
        $("#detail2").val(data[1])
        $("#skillList2").val(data[3])
        $("#workedyears2").val(data[2])
        $("#requiredNum2").val(data[4])



        $("#positionDetailModal").modal("show")
    })
    //职位详情


    //选择技能开始
    $("#skillAddbtn").on("click", function(){
         //请求
                $.ajax({
                    url: "/job/position/showAllSkill",
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        $("#skillTbody").empty();
                        $(data.data.dictValue).each(function (j) {
                            $("#skillTbody").append(
                            " <tr><td>"
                                +" <input type='checkbox' name='checkskill' value='"+this.label+"'>"
                                + "</td><td>"
                                + this.id + "</td><td>"
                                + this.label + "</td></tr>"
                            )
                        })

                    $("#SkillModal").modal("show")
                    },
                    error: function (err) {
                        alert("失败")
                    }
                })
                //请求end

    })
    //选择技能结束

    //选择技能确认开始
    $("#skillAddbtn2").on("click", function(){
        var checkSkills = '';
        $("input[name='checkskill']:checked").each(function(i){
            checkSkills = checkSkills + $(this).val() + ';';
        });
        $('#skills').val(checkSkills);
        $("#SkillModal").modal("hide")
    })
    //选择技能确认结束


    //修改职位模态框赋值
    $("body").on("click", "button[name='positionEdit']", function(){
        var dataArr = $(this).attr("id").split("_")
        $("#id3").val(dataArr[0])
        $("#name3").val(dataArr[1])
        $("#minSales3").val(dataArr[2])
        $("#maxSales3").val(dataArr[3])
        $("#city3").val(dataArr[4])
        $("#address3").val(dataArr[5])
        $("#workedyears3").val(dataArr[6])
        $("#education3").val(dataArr[7])
        $("#requiredNum3").val(dataArr[8])
        $("#detail3").val(dataArr[9])
        $("#skills3").val(dataArr[10])

        $("#positionEditModal").modal("show")
    })
    //修改职位模态框赋值end

    //修改职业
    $("#positionAdd2").on("click", function(){
        let Data = {"id":$("#id3").val(), "name":$("#name3").val(), "minSales":$("#minSales3").val(), "maxSales":$("#maxSales3").val(),
            "city":$("#city3").val(), "address":$("#address3").val(), "pid":$("#pid3").val(),
            "workedyears":$("#workedyears3").val(), "education":$("#education3").val(),
            "requiredNum":$("#requiredNum3").val(),"detail":$("#detail3").val(), "skillList":$("#skills3").val()}
        //请求
        $.ajax({
            url : "position/editPosition",
            data : JSON.stringify(Data),
            type : "post",
            contentType: 'application/json;charset=utf-8',
            dataType : "json",
            success : function(data){
                alert("成功")
                $("#positionEditModal").modal("hide")
                showAll();
            }
        })
        //请求end
    })
    //修改职业end

    //删除职位
    $("body").on("click", "button[name='positionDelete']", function(){
        var $id = $(this).attr("id");

        if (confirm("确认删除吗？")){
            //请求
            $.ajax({
                url : "position/deletePosition/" + $id,
                type : "post",
                dataType : "json",
                success : function(data){
                    showAll();
                }
            })
            //请求end
        }

    })
    //删除职位end

});